package com.sunshine.litemusic.fragment.search;

import android.inputmethodservice.Keyboard;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.sunshine.litemusic.R;
import com.sunshine.litemusic.activity.main.MainActivity;
import com.sunshine.litemusic.callback.MyCallBack;

import java.util.ArrayList;

/**
 * @author sunshine
 * @date 2020/5/29
 */
public class SearchFragment extends Fragment {

    private SearchViewModel viewModel;
    private MainActivity activity;
    private boolean isSearch = true;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_search, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel = new ViewModelProvider(this).get(SearchViewModel.class);
        activity = (MainActivity) getActivity();

        //如果不是搜索界面则隐藏搜索单元
        LinearLayout linearLayout = getView().findViewById(R.id.ll_search);
        final TextInputLayout textInputLayout = getView().findViewById(R.id.til_search);
        final TextInputEditText editText = getView().findViewById(R.id.edit_search);
        Button button = getView().findViewById(R.id.button_search);
        if (isSearch){
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        if (null == editText.getText()){
                            textInputLayout.setError("请输入歌单id");
                        }else {
                            textInputLayout.setErrorEnabled(false);
                            int id = Integer.parseInt(editText.getText().toString());
                            viewModel.getMusic(id);
                        }
                    }catch (Exception e){
                        textInputLayout.setError("id有错误");
                    }

                }
            });
        }else {
            linearLayout.setVisibility(View.GONE);
            viewModel.getMusic();
        }

        final RecyclerView recyclerView = getView().findViewById(R.id.recyclerView_music);
        viewModel.getSongBeanMutableLiveData().observe(getViewLifecycleOwner(), new Observer<ArrayList<SearchModel.SearchBean>>() {
            @Override
            public void onChanged(ArrayList<SearchModel.SearchBean> searchBeans) {
                if (null != searchBeans){
                    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                    recyclerView.setAdapter(new SearchAdapter(searchBeans, new MyCallBack() {
                        @Override
                        public <T, S> void callBack(T beanList, S position) {
                            activity.searchToPlay((ArrayList<SearchModel.SearchBean>) beanList, (Integer) position);
                        }
                    }));
                }
            }
        });
    }

    /**
     * 设置当前界面是否为搜索界面
     * 复用界面减少工作量
     * 默认为是搜索界面
     * @param search 是否为搜索界面
     */
    public void setSearch(boolean search){
        this.isSearch = search;
    }
}
